#include <stdio.h>
#include <stdlib.h>
#include "modularity_mat.h"

#if 0
 /*extern int createSparseMatrixFromFile(char *sFilePath, sparse_matrix_lst **pMatrix, int *pMatrixSize);*/
int main(int argc, char **argv)
{
	FILE* GroupFile;
	elem **GnrModMat, **ModMat;
	int *GroupVertexDegrees, *Group;
	int iMatrixSize,iGroupSize = 0, i, j, M,iTemp=0;
	elem* F_g;  
	sparse_matrix_lst *A, *A_g;

	if (argc != 3) 
	{
		return 0;
	}

	/*generate A_sparse	*/
	if (createSparseMatrixFromFile(argv[1],&A,&iMatrixSize) == 0)
	{
		FreeMem1(GnrModMat,ModMat,GroupVertexDegrees,Group,F_g,A,A_g,iGroupSize);
		return 0;
	}

	/*generate Group array*/
	GroupFile = fopen(argv[2],"r");
	if (GroupFile == NULL)
	{
		FreeMem1(GnrModMat,ModMat,GroupVertexDegrees,Group,F_g,A,A_g,iGroupSize); 
		return 0;
	}

	/*zeroizing counter*/
	i = 0;
	iGroupSize = 0;
	/* read g from the VertexGroup File to determine group size*/
	while (fscanf(GroupFile, "%d", &iTemp) == 1)  
	{
		iGroupSize++;
	}
	/*rewind to beginning of file, */
	rewind(GroupFile);
	/* allocate array indicating indices participating in group */
	Group = (int *)(calloc(iGroupSize, sizeof(int)));
	if (Group == NULL)
	{
		FreeMem1(GnrModMat,ModMat,GroupVertexDegrees,Group,F_g,A,A_g, iGroupSize);
		return 0;
	}
	/*read file again, and fill Group array*/
	while (fscanf(GroupFile, "%d", &iTemp) == 1)  
	{
		if (iTemp >= A->n)
		{
			printf("invalid vertex number (group file) [%d]",iTemp);
			FreeMem1(GnrModMat,ModMat,GroupVertexDegrees,Group,F_g,A, A_g, iGroupSize);
			return 0;
		}
		Group[i] = iTemp;   
		i++;
	}

	if (Group == NULL)
	{
		FreeMem1(GnrModMat,ModMat,GroupVertexDegrees,Group,F_g,A, A_g, iGroupSize);
		return 0;
	}	

	/*calculate the modularity matrix*/
	if (CreateModularityMat(A,Group,iGroupSize,&ModMat,&GroupVertexDegrees,&M) == 0)
	{
		FreeMem1(GnrModMat,ModMat,GroupVertexDegrees,Group,F_g,A, A_g, iGroupSize);
		return 0;
	}

	/*calculate the GENERALIZED modularity matrix*/
	if (CreateGeneralizedModMat(ModMat,Group,iGroupSize,&GnrModMat,&F_g) == 0)
	{
		FreeMem1(GnrModMat,ModMat,GroupVertexDegrees,Group,F_g,A, A_g, iGroupSize);
		return 0;
	}

	/*print matrix size*/
	printf("%d",iGroupSize);
	puts(" ");
	/*print generalized modularity matrix to the std output, elements ordered by row*/
	for (i=0; i<iGroupSize; i++)
	{
		for (j=0; j<iGroupSize; j++) 
		{
			printf("%.3f ",GnrModMat[i][j]);
		}
		puts(" ");
	}

	free(F_g);
	free(GroupVertexDegrees);
	free(Group);
	Free2dimensionalArray(GnrModMat,iGroupSize);
	Free2dimensionalArray(ModMat,iGroupSize);
	free_sparse_matrix_lst(A);

	return 1;
}
#endif